/*
 * @Author: zi.yang
 * @Date: 2021-03-15 07:54:35
 * @LastEditors: zi.yang
 * @LastEditTime: 2021-03-20 18:35:54
 * @Description: 数据持久层
 * @FilePath: \vue-music\src\components\common\storage.ts
 */
interface ISetItem {
  key: string,
  data: object
}
interface IGetItem {
  key: string,
  success: (data: Array<string>) => void
}
interface IRemoveItem {
  key: string,
  success: () => void
}
class Storage {
  // 设置存储
  setItem({ key, data }: ISetItem): void {
    window.localStorage.setItem(key, JSON.stringify(data));
  }

  // 获取指定存储内容
  getItem = ({ key, success }: IGetItem): void => {
    const data: string | null = window.localStorage.getItem(key);
    if (data === null) {
      return;
    }
    success(JSON.parse(data));
  }

  // 删除指定存储内容
  removeItem = ({ key, success }: IRemoveItem): void => {
    window.localStorage.removeItem(key);
    success();
  }
}

export default Storage;
